

#import "UIView+Extension.h"

@implementation UIView (XMGExtension)

- (void)setMy_Size:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)My_Size
{
    return self.frame.size;
}

- (void)setMy_Width:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (void)setMy_Height:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (void)setMy_X:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (void)setMy_Y:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (void)setMy_CenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (void)setMy_CenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)My_CenterY
{
    return self.center.y;
}

- (CGFloat)My_CenterX
{
    return self.center.x;
}

- (CGFloat)My_Width
{
    return self.bounds.size.width;
}

- (CGFloat)My_Height
{
    return self.bounds.size.height;
}

- (CGFloat)My_X
{
    return self.frame.origin.x;
}

- (CGFloat)My_Y
{
    return self.frame.origin.y;
}


- (BOOL)isShowingOnKeyWindow
{
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [self.superview convertRect:self.frame toView:keyWindow];
    CGRect winBounds = keyWindow.bounds;
    
    // 主窗口的bounds 和 self的矩形框 是否有重叠
    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
    
    return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}


/**
 *  根据nib文件来创建view
 *
 */
+ (instancetype)viewForNib{
    
   return  [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil] lastObject];
}

@end
